package com.dld.coupon.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.ImageView;

import com.dld.android.net.HttpUtil;
import com.dld.android.util.LogUtils;
import com.dld.coupon.activity.ActivityManager;
import com.dld.protocol.image.ImageCache;
import com.dld.protocol.image.ImageProtocol;
import com.dld.protocol.json.User;
import com.tencent.weibo.utils.Cache;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.lang.ref.SoftReference;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FileUtil {
    public static final String BACKUPUHUODONG_SDCARD_PATH;
    public static final String LAUNCHLOGO_PATH;
    private static Handler handler = new Handler(Looper.getMainLooper());
    public static ExecutorService pool = Executors.newFixedThreadPool(3);

    static {
        BACKUPUHUODONG_SDCARD_PATH = Environment.getExternalStorageDirectory()
                + "/dld/huodongcache";
        LAUNCHLOGO_PATH = Environment.getExternalStorageDirectory()
                + "/dld/huodongcache/";
    }

    public static void deleteGroupImage(String paramString) {
        deleteImage(paramString, 200, 200);
        deleteImage(paramString, 400, 400);
    }

    private static void deleteImage(String paramString, int paramInt1,
            int paramInt2) {
        try {
            String str = genFileName(paramString, paramInt1, paramInt2);
            new File(new File(Environment.getExternalStorageDirectory(),
                    "dld"), str).delete();
            return;
        } catch (Exception localException) {
            while (true)
                LogUtils.e("test", "", localException);
        }
    }

    public static void deleteTicketImage(String paramString) {
        deleteImage(paramString, 140, 200);
        deleteImage(paramString, 600, 600);
    }

    public static String genFileName(String paramString, int paramInt1,
            int paramInt2) {
        return paramInt1 + "_" + paramInt2 + "_"
                + URLEncoder.encode(paramString).replaceAll(":|/", "_");
    }

    // ERROR //
    public static Bitmap getBitmapFromInputStream(InputStream paramInputStream) {
        // Byte code:
        // 0: new 136 java/io/ByteArrayOutputStream
        // 3: dup
        // 4: invokespecial 137 java/io/ByteArrayOutputStream:<init> ()V
        // 7: astore_1
        // 8: aconst_null
        // 9: astore_2
        // 10: aload_0
        // 11: invokevirtual 143 java/io/InputStream:read ()I
        // 14: istore_3
        // 15: iload_3
        // 16: bipush 255
        // 18: if_icmpne +39 -> 57
        // 21: aload_1
        // 22: invokevirtual 147 java/io/ByteArrayOutputStream:toByteArray ()[B
        // 25: astore_2
        // 26: aload_2
        // 27: iconst_0
        // 28: aload_2
        // 29: arraylength
        // 30: invokestatic 153 android/graphics/BitmapFactory:decodeByteArray
        // ([BII)Landroid/graphics/Bitmap;
        // 33: astore_2
        // 34: aconst_null
        // 35: checkcast 155 [B
        // 38: pop
        // 39: aload_0
        // 40: ifnull +7 -> 47
        // 43: aload_0
        // 44: invokevirtual 158 java/io/InputStream:close ()V
        // 47: aload_1
        // 48: ifnull +7 -> 55
        // 51: aload_1
        // 52: invokevirtual 159 java/io/ByteArrayOutputStream:close ()V
        // 55: aload_2
        // 56: areturn
        // 57: aload_1
        // 58: iload_3
        // 59: invokevirtual 163 java/io/ByteArrayOutputStream:write (I)V
        // 62: aload_0
        // 63: invokevirtual 143 java/io/InputStream:read ()I
        // 66: istore_3
        // 67: iload_3
        // 68: istore_3
        // 69: goto -54 -> 15
        // 72: astore_3
        // 73: aload_3
        // 74: invokevirtual 166 java/io/IOException:printStackTrace ()V
        // 77: aload_0
        // 78: ifnull +7 -> 85
        // 81: aload_0
        // 82: invokevirtual 158 java/io/InputStream:close ()V
        // 85: aload_1
        // 86: ifnull -31 -> 55
        // 89: aload_1
        // 90: invokevirtual 159 java/io/ByteArrayOutputStream:close ()V
        // 93: goto -38 -> 55
        // 96: astore_1
        // 97: aload_1
        // 98: invokevirtual 167 java/lang/Exception:printStackTrace ()V
        // 101: goto -46 -> 55
        // 104: astore_3
        // 105: aload_3
        // 106: invokevirtual 167 java/lang/Exception:printStackTrace ()V
        // 109: goto -24 -> 85
        // 112: astore_2
        // 113: aload_0
        // 114: ifnull +7 -> 121
        // 117: aload_0
        // 118: invokevirtual 158 java/io/InputStream:close ()V
        // 121: aload_1
        // 122: ifnull +7 -> 129
        // 125: aload_1
        // 126: invokevirtual 159 java/io/ByteArrayOutputStream:close ()V
        // 129: aload_2
        // 130: athrow
        // 131: astore_3
        // 132: aload_3
        // 133: invokevirtual 167 java/lang/Exception:printStackTrace ()V
        // 136: goto -15 -> 121
        // 139: astore_1
        // 140: aload_1
        // 141: invokevirtual 167 java/lang/Exception:printStackTrace ()V
        // 144: goto -15 -> 129
        // 147: astore_3
        // 148: aload_3
        // 149: invokevirtual 167 java/lang/Exception:printStackTrace ()V
        // 152: goto -105 -> 47
        // 155: astore_1
        // 156: aload_1
        // 157: invokevirtual 167 java/lang/Exception:printStackTrace ()V
        // 160: goto -105 -> 55
        //
        // Exception table:
        // from to target type
        // 10 39 72 java/io/IOException
        // 57 67 72 java/io/IOException
        // 89 93 96 java/lang/Exception
        // 81 85 104 java/lang/Exception
        // 10 39 112 finally
        // 57 67 112 finally
        // 73 77 112 finally
        // 117 121 131 java/lang/Exception
        // 125 129 139 java/lang/Exception
        // 43 47 147 java/lang/Exception
        // 51 55 155 java/lang/Exception
        return null;
    }

    // ERROR //
    public static byte[] getByteFromInputStream(InputStream paramInputStream) {
       
        return null;
    }

    // ERROR //
    public static Bitmap getDrawbleInCache(String paramString) throws Exception {
       
        return null;
    }

    // ERROR //
    public static byte[] getFileContent(String paramString) {
       
        return null;
    }

    public static User getUserInfo() throws IOException {
        Object localObject = new File(new File(
                Environment.getExternalStorageDirectory(), "dld"),
                "userinfo");
        Properties localProperties = new Properties();
        User localUser = new User();
        localObject = new FileInputStream((File) localObject);
        localProperties.load((InputStream) localObject);
        localUser.email = localProperties.getProperty("email", null);
        localUser.mobile = localProperties.getProperty("mobile", null);
        localUser.username = localProperties.getProperty("username", null);
        localUser.gender = new Integer(localProperties.getProperty("gender",
                "0")).intValue();
        localUser.qqcode = localProperties.getProperty("qqcode", null);
        localUser.msn = localProperties.getProperty("msn", null);
        localUser.weibo = localProperties.getProperty("weibo", null);
        localUser.idcard = localProperties.getProperty("idcard", null);
        localUser.cityname = localProperties.getProperty("cityname", null);
        localUser.lastLoginTime = localProperties.getProperty("lastLoginTime",
                null);
        localUser.registerDate = localProperties.getProperty("registerDate",
                null);
        ((FileInputStream) localObject).close();
        return (User) localUser;
    }

    // ERROR //
    public static byte[] readFile(String paramString) {
       
        return null;
    }

    public static void readImage(final ImageView paramImageView,
            final String paramString, int paramInt1, int paramInt2) {
        if (paramString != null)
            try {
                if (ImageCache.getInstance().get(paramString) != null) {
                    paramImageView.setImageBitmap(ImageCache.getInstance().get(
                            paramString));
                    Log.e("main", "get url in cache");
                    paramImageView.setTag(Tag.IMG_CHANGED);
                }
                // while (true)
                {
                    Log.e("main", "cache size==" + ImageCache.mSoftCache.size());
                    // break;
                    Object localObject = readFile(genFileName(paramString,
                            paramInt1, paramInt2));
                    localObject = new SoftReference(
                            BitmapFactory.decodeByteArray((byte[]) localObject,
                                    0, ((byte[]) localObject).length));
                    ImageCache.getInstance();
                    ImageCache.mSoftCache.put(paramString,
                            (SoftReference<Bitmap>) localObject);
                    Log.e("main", "decode ");
                    pool.submit(new Runnable() {
                        public void run() {
                            FileUtil.handler.post(new Runnable() {
                                public void run() {
                                    paramImageView
                                            .setImageBitmap((Bitmap) ImageCache
                                                    .getInstance().get(
                                                            paramString));
                                }
                            });
                        }
                    });
                    paramImageView.setTag(Tag.IMG_CHANGED);
                }
            } catch (Exception localException) {
                Log.e("main", "readImage exception");
                new ImageProtocol(ActivityManager.getCurrent(),
                        "http://www.dld.com/tuan/viewimage?u="
                                + URLEncoder.encode(paramString) + "&w="
                                + paramInt1 + "&h=" + paramInt2)
                        .startTrans(paramImageView);
            }
    }

    public static void saveFile(String paramString, byte[] paramArrayOfByte) {
        saveFile(paramString, paramArrayOfByte, false);
    }

    // ERROR //
    public static void saveFile(String paramString, byte[] paramArrayOfByte,
            boolean paramBoolean) {
      
    }

    public static void saveGroupImage(String paramString) {
        saveImage(paramString, 200, 200);
        saveImage(paramString, 400, 400);
    }

    private static void saveImage(final String paramString,
            final int paramInt1, final int paramInt2) {
        try {
            byte[] arrayOfByte = (byte[]) Cache
                    .getCache("http://www.dld.com/tuan/viewimage?u="
                            + URLEncoder.encode(paramString) + "&w="
                            + paramInt1 + "&h=" + paramInt2);
            if ((arrayOfByte == null) || (arrayOfByte.length < 10))
                new Thread() {
                    public void run() {
                        try {
                            byte[] arrayOfByte = FileUtil
                                    .getByteFromInputStream(HttpUtil
                                            .getInputStreamByUrl(paramString,
                                                    ActivityManager
                                                            .getCurrent()));
                            if ((arrayOfByte != null)
                                    && (arrayOfByte.length >= 0))
                                FileUtil.saveFile(FileUtil.genFileName(
                                        paramString, paramInt1, paramInt2),
                                        arrayOfByte);
                        } catch (Exception localException) {
                            localException.printStackTrace();
                        }
                    }
                };
            else
                saveFile(genFileName(paramString, paramInt1, paramInt2),
                        arrayOfByte);
        } catch (Exception localException) {
            LogUtils.e("test", "", localException);
        }
    }

    public static void saveTicketImage(String paramString) {
        saveImage(paramString, 140, 200);
        saveImage(paramString, 600, 600);
    }

    public static boolean saveUserInfo(User paramUser) throws IOException {
        File localFile = new File(new File(
                Environment.getExternalStorageDirectory(), "dld"),
                "userinfo");
        Properties localProperties = new Properties();
        new OutputStreamWriter(new PrintStream(localFile), "UTF-8");
        localProperties.setProperty("email", paramUser.email);
        localProperties.setProperty("mobile", paramUser.mobile);
        localProperties.setProperty("username", paramUser.username);
        localProperties.setProperty("realname", paramUser.realname);
        localProperties.setProperty("gender", String.valueOf(paramUser.gender));
        localProperties.setProperty("qqcode", paramUser.qqcode);
        localProperties.setProperty("msn", paramUser.msn);
        localProperties.setProperty("weibo", paramUser.weibo);
        localProperties.setProperty("idcard", paramUser.idcard);
        localProperties.setProperty("cityname", paramUser.cityname);
        localProperties.setProperty("lastLoginTime", paramUser.lastLoginTime);
        localProperties.setProperty("registerDate", paramUser.registerDate);
        return true;
    }

    // ERROR //
    public static boolean writehuodongCache(InputStream paramInputStream,
            String paramString) throws IOException {
      
        return false;
    }
}
